package com.ruoyi.archive.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.archive.domain.ArchiveCar;
import com.ruoyi.archive.service.IArchiveCarService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆信息Controller
 *
 * @author yepanpan
 * @date 2025-04-18
 */
@RestController
@RequestMapping("/archive/car")
public class ArchiveCarController extends BaseController
{
    @Autowired
    private IArchiveCarService archiveCarService;

    /**
     * 查询车辆信息列表
     */
    @GetMapping("/select")
    public TableDataInfo select(ArchiveCar archiveCar)
    {
        startPage();
        List<ArchiveCar> list = archiveCarService.selectArchiveCarList(archiveCar);
        return getDataTable(list);
    }

    /**
     * 查询车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('archive:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArchiveCar archiveCar)
    {
        startPage();
        List<ArchiveCar> list = archiveCarService.selectArchiveCarList(archiveCar);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('archive:car:export')")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveCar archiveCar)
    {
        List<ArchiveCar> list = archiveCarService.selectArchiveCarList(archiveCar);
        ExcelUtil<ArchiveCar> util = new ExcelUtil<ArchiveCar>(ArchiveCar.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    @Log(title = "车辆信息管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('archive:car:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ArchiveCar> util = new ExcelUtil<ArchiveCar>(ArchiveCar.class);
        List<ArchiveCar> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = archiveCarService.importData(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ArchiveCar> util = new ExcelUtil<ArchiveCar>(ArchiveCar.class);
        util.importTemplateExcel(response, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('archive:car:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(archiveCarService.selectArchiveCarById(id));
    }

    /**
     * 新增车辆信息
     */
    @PreAuthorize("@ss.hasPermi('archive:car:add')")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ArchiveCar archiveCar)
    {
        archiveCar.setCreateBy(getUsername());
        return toAjax(archiveCarService.insertArchiveCar(archiveCar));
    }

    /**
     * 修改车辆信息
     */
    @PreAuthorize("@ss.hasPermi('archive:car:edit')")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ArchiveCar archiveCar)
    {
        archiveCar.setUpdateBy(getUsername());
        return toAjax(archiveCarService.updateArchiveCar(archiveCar));
    }

    /**
     * 删除车辆信息
     */
    @PreAuthorize("@ss.hasPermi('archive:car:remove')")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(archiveCarService.deleteArchiveCarByIds(ids));
    }
}
